package com.sbw.java.advanced.demo.common.exceptions;

import com.oyo.common.enums.ResponseCode;
import com.oyo.common.response.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResponse handler(Exception e) {
        logger.error(e.getMessage(), e);
        if (e instanceof BusinessException) {
            return BaseResponse.fail(((BusinessException) e).getResponseCode(), ((BusinessException) e).getResponseMsg());
        }
        return BaseResponse.fail(ResponseCode.FAILURE.getCode(), e.getMessage());
    }


}
